// pages/module/addProduct/addProduct.js
import regeneratorRuntime from '../../../utils/runtime.js';
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import util from '../../../utils/util.js';
import {
  getLiveProduct,
  addProductToLive,
  live_goods_list
} from '../../../utils/imapi.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_confirm:false,
    productList:[], //所有的商品列表
    dotList:[], //已经选择的 商品
    live_id:'',
	Back:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    console.log(options.Back);
    console.log("===参数",options)
    this.setData({
		Back:options.Back
	})

    var id = options.id || ''
    var auditedData = await getLiveProduct({audit_status:2}) || {}
    this.last_productList = auditedData.data;
    this.setData({
      live_id:id,
      productList:auditedData.data,
    })

    this.loadDotList();
	//this.selectComponent("#liss").getList();
  },

  async loadDotList(e){ // 加载已经选择的; 待处理
   // {} 是取得树形
   //  该业务 需要 加载 已经导入得商品 标记出来。
     let {data} =await live_goods_list({lv_record_id:this.data.live_id,page_index:1,page_size:100})
     if (data!=null && data.length>0){

        let productList= this.data.productList
        let dt=[];
		console.log(data);
        for( var i=0;i<data.length;i++){
          data[i].is_select=1
		 // console.log(this.data.productList)
            var index=this.data.productList.findIndex(item=>{
				return item.goods_title==data[i].title
			})
			//console.log(index)
		    dt.push(this.data.productList[index]);
        }
		//console.log(dt)
        this.setData({
          dotList:dt
          //productList:productList,
        })
        console.log(this.data.dotList)
       // this.data.productList[index].is_select = 0;  productList也需要设置



       
     }
     



  },

  addPro(e) { // 添加到直播间
    let {index,imgInfo} = e.detail;
    console.log(e);
    
    this.data.productList[index].is_select = 1;
    this.last_productList = this.data.productList
		this.setData({
      productList:this.data.productList,
      dotImage: this.data.productList[index].pict_main_url 
    });
    let index1 = this.data.dotList.length;
    
    let flag = this.data.dotList.findIndex(item=>item.id == imgInfo.id);
	console.log(flag);
    if(flag > -1){
      let data = this.data.dotList;
      data.splice(flag,1);
      this.setData({
        dotList: data
      });
    }else{
	  //let index=this.data.productList.findIndex(item=>item.id == imgInfo.id);
		//console.log(imgInfo.id);
      this.setData({
        dotList: [
          ...this.data.dotList,
          imgInfo
        ]
		//dotList:dotList
      });
    }
    //console.log(this.data.dotList);
	
    let destination = this.destination || {};
    let dotList = this.data.dotList;
    if (dotList[index1]){
      dotList[index1].top = destination.top-10;
      dotList[index1].left = destination.left;
      dotList[index1].scale = 0;
    }
    
    this.setData({
      dotList: dotList
    });
    setTimeout(() => {
      let dotList = this.data.dotList;
      if(!(dotList && dotList[index])) return;
      dotList[index].show = false;
      this.setData({
        dotList: dotList
      });
    }, 600)
  },


  removePro(e){ // 选择商品移除
    let {index} = e.detail;
    this.data.productList[index].is_select = 0;
    let _dotList = this.data.dotList
    for(var i = 0; i < _dotList.length; i++){
      if(_dotList[i].item_id==this.data.productList[index].item_id){
        _dotList.splice(i,1)
      }
    }
    this.last_productList = this.data.productList
    this.setData({
      productList:this.data.productList,
      dotList:_dotList
    })
  },
  removePro1(e){
    let {index} = e.detail;
    let _dotList = this.data.productList
    this.last_productList.forEach(item=>{
      if(item.item_id == _dotList[index].item_id){
        item.is_select = 0;
      }
    })
    _dotList.splice(index,1)
    this.setData({
      productList:_dotList
    })
  },
  async confirm(){ //直播提交
    var new_arr = '';
    this.data.dotList.forEach(item=>{
      new_arr += (item.id +',') //  商品的 id 逗号隔开， 需要加入到的直播商品
    })
    new_arr = new_arr.slice(0,new_arr.length-1)

    var new_arr_ref_goods_id = '';
    this.data.dotList.forEach(item=>{
      new_arr_ref_goods_id += (item.ref_goods_id +',') //  商品的 id 逗号隔开， 需要加入到的直播商品
    })
    new_arr_ref_goods_id = new_arr_ref_goods_id.slice(0,new_arr_ref_goods_id.length-1)
    console.log("=== 外部直播间商品id:"+new_arr_ref_goods_id)
    

    wx.showLoading({
      title: '导入商品到直播间中...',
    })

    await addProductToLive({
      lv_record_id:this.data.live_id,
      goods_id:new_arr,
      ref_goods_id:new_arr_ref_goods_id
    })
    
    wx.hideLoading({
      success: (res) => {},
    })
    
      wx.showToast({
        title: '商品已添加到直播间',
      })


      util.go_history_page('pages/module/liveManagement/liveManagement',true)
     
    
  },
  goConfirm(){

    let that=this
    if(this.data.dotList.length==0){

      wx.showModal({
        title: '未选择商品',
        content:'是否不需要选择商品？',
        success:function(res){
            if(res.confirm){

              that.setData({
                is_confirm:true,
                productList:that.data.dotList,
              })
            }else if(res.cancel){

            }

        },
        fail:function(res){

        }
      })

    }else{
      that.setData({
        is_confirm:true,
        productList:that.data.dotList,
      })
    }

   
  },
  showAdd(){
    this.data.dotList = this.data.productList
    this.setData({
      dotList:this.data.dotList,
      is_confirm:false,
      productList:this.last_productList,
    })
  },
  upPro(e){
    let {index} = e.detail;
    if(index==0) return;
    let up_pro = this.data.productList[index-1]
    let cur_pro = this.data.productList[index]
    let newArr = this.data.productList;
    newArr[index-1] = cur_pro;
    newArr[index] = up_pro;
    this.setData({
      productList:newArr
    })
  },
  downPro(e){
    let {index} = e.detail;
    if(index==this.data.productList.length-1) return;
    let down_pro = this.data.productList[index+1]
    let cur_pro = this.data.productList[index]
    let newArr = this.data.productList;
    newArr[index+1] = cur_pro;
    newArr[index] = down_pro;
    this.setData({
      productList:newArr
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.createSelectorQuery().select(".left_add").boundingClientRect((res) => {
			that.destination = res;
		}).exec();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  go_btn_search_all:function(){

    wx.navigateTo({
      url: '/pages/module/proManagement/proManagement',
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})